package com.kangyi.app.service.impl;

import com.kangyi.app.mapper.NursingHomeFavoriteMapper;
import com.kangyi.app.service.FavoriteService;
import com.kangyi.constant.MessageConstant;
import com.kangyi.dto.ToggleFavoriteDTO;
import com.kangyi.entity.NursingHome;
import com.kangyi.entity.NursingHomeFavorite;
import com.kangyi.exception.AddException;
import com.kangyi.exception.DataNotExistException;
import com.kangyi.utils.AuthUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @author Blank
 * @version 1.0
 */
@Service
public class FavoriteServiceImpl implements FavoriteService {

    @Autowired
    private NursingHomeFavoriteMapper nursingHomeFavoriteMapper;

    @Override
    public void add(ToggleFavoriteDTO toggleFavoriteDTO) {
        NursingHomeFavorite Favorite =nursingHomeFavoriteMapper.select(toggleFavoriteDTO.getUserId(),toggleFavoriteDTO.getNursingHomeId());
        if (Favorite != null) {
            throw new AddException(MessageConstant.COLLECTED_NURSING_HOME);
        }

        NursingHomeFavorite nursingHomeFavorite = new NursingHomeFavorite();
        BeanUtils.copyProperties(toggleFavoriteDTO,nursingHomeFavorite);
        nursingHomeFavoriteMapper.addFavorite(nursingHomeFavorite);
    }

    @Override
    public void remove(ToggleFavoriteDTO toggleFavoriteDTO) {
        Integer userId = toggleFavoriteDTO.getUserId();
        AuthUtil.validateUserPermission(userId);
        nursingHomeFavoriteMapper.cancelFavorite(toggleFavoriteDTO);
    }
}
